package com.mid;

/**
 * Created by Lxk on 2019/6/3.
 */
public class Solution142 {

    public ListNode detectCycle(ListNode head) {
        if(head==null){
            return null;
        }
        ListNode faster = head,slower = head;
        while(faster!=null){
            faster = faster.next;
            slower = slower.next;
            if(faster!=null){
                faster = faster.next;
            }
            if(faster==slower){
                break;
            }
        }
        if(faster==null){
            return null;
        }
        faster = head;
        while(faster!=slower){
            faster = faster.next;
            slower = slower.next;
        }
        return slower;
    }

}
